package workflowminer.views;

import java.util.Enumeration;
import java.util.Vector;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import workflowminer.domain.VarData;
import workflowminer.load.ModelViewComunicator;
import workflowminer.views.Ventana.fileExitItemListener;
import workflowminer.views.Ventana.fileOpenListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

public class Ventanita {

	private Table table = null;

	private Menu menuBar = null;

	private MenuItem fileMenuHeader;

	private Menu fileMenu; // @jve:decl-index=0:

	private MenuItem fileAddItem;

	private MenuItem fileExitItem;

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="24,33"

	private MenuItem fileMenuHeader1;

	private MenuItem fileMenuHeader3;

	private Canvas canvas = null;  //  @jve:decl-index=0:visual-constraint="539,377"

	private Combo combo = null;

	private Label label = null;

	private Button button = null;

	private Text textArea = null;

	public void init() {
		this.createSShell();
	}

	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		sShell = new Shell();
		sShell.setText("WF MINER");
		sShell.setLayout(null);
		sShell.setSize(new Point(1069, 857));
		Label filler = new Label(sShell, SWT.NONE);
		filler.setBounds(new Rectangle(5, 79, 0, 15));
		button = new Button(sShell, SWT.NONE);
		button.setBounds(new Rectangle(67, 310, 185, 25));
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
			}
		});
		menuBar = new Menu(sShell, SWT.BAR);
		sShell.setMenuBar(menuBar);
		menuBar.setVisible(true);
		fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuHeader.setText("&File");
		fileMenuHeader1 = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuHeader1.setText("&x");
		fileMenuHeader3 = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuHeader3.setText("&x");
		fileMenu = new Menu(sShell, SWT.DROP_DOWN);
		fileMenuHeader.setMenu(fileMenu);
		fileAddItem = new MenuItem(fileMenu, SWT.PUSH);
		fileAddItem.setText("&Open");
		fileAddItem.addSelectionListener(new fileOpenListener());
		fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
		fileExitItem.setText("&Exit");
		fileExitItem.addSelectionListener(new fileExitItemListener());

		table = new Table(sShell, SWT.NONE);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(new Font(Display.getDefault(), "Arial", 12, SWT.NORMAL));
		table.setBounds(new Rectangle(13, 77, 311, 220));
		createCanvas();
		createCombo();
		label = new Label(sShell, SWT.CENTER);
		label.setBounds(new Rectangle(20, 25, 120, 24));
		label.setFont(new Font(Display.getDefault(), "Tahoma", 12, SWT.BOLD));
		label.setText("Variable");
		textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		textArea.setBounds(new Rectangle(394, 71, 612, 385));
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("Variable");
		TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
		tableColumn1.setWidth(100);
		tableColumn1.setText("Valor");
		TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
		tableColumn2.setWidth(110);
		tableColumn2.setText("Ocurrencias");
		/*
		 * for(int i=1;i<60;i++){ addArticuloToTable(1045,"Grande","Pito de
		 * goma","PORONGOX",69.69,8); }
		 */
		sShell.open();
		while (!sShell.isDisposed()) {
			if (!sShell.getDisplay().readAndDispatch())
				sShell.getDisplay().sleep();
		}
	}

	public void addArticuloToTable(String var, String value, int cant) {
		TableItem item = new TableItem(table, SWT.NULL);
		item.setText("Item ");
		item.setText(0, " " + var);
		item.setText(1, value);
		item.setText(2, "" + cant);
	}

	class fileExitItemListener implements SelectionListener {
		public void widgetSelected(SelectionEvent event) {
			sShell.close();
		}

		public void widgetDefaultSelected(SelectionEvent e) {

		}
	}
	
	class fileOpenListener implements SelectionListener {
		public void widgetSelected(SelectionEvent event) {
			FileDialog fdialog = new FileDialog(sShell);
			fdialog.open();
			ModelViewComunicator.getMwc().load(fdialog.getFilterPath()+"\\"+fdialog.getFileName());
		}

		public void widgetDefaultSelected(SelectionEvent e) {

		}
	}
	
	public void showInMainTable(Vector vector) {
		Enumeration e = vector.elements();
		this.clearMainTable();
		while (e.hasMoreElements()) {
			VarData v = (VarData) e.nextElement();
			this.addArticuloToTable(v.getVarName(), v.getValue(), v
					.getOcurrencies());
		}
	}

	public void clearMainTable() {
		this.table.clearAll();
		this.table.setItemCount(0);
	}

	/**
	 * This method initializes canvas
	 * 
	 */
	private void createCanvas() {
		canvas = new Canvas(sShell, SWT.NONE);
		canvas.setBounds(new Rectangle(336, 22, 593, 426));
	}

	public void loadChart() {
		ImageData imageData = new ImageData("grafico.jpg");
		final Image fullImage = new Image(sShell.getDisplay(), imageData);
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(fullImage, 0, 0);
			}
		});
		canvas.redraw();
	}

	/**
	 * This method initializes combo	
	 *
	 */
	private void createCombo() {
		combo = new Combo(sShell, SWT.NONE);
		combo.setFont(new Font(Display.getDefault(), "Tahoma", 12, SWT.NORMAL));
		combo.setBounds(new Rectangle(155, 23, 164, 27));
		combo.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				ModelViewComunicator.getMwc().showVariable(combo.getText()); 
			}
			public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
			}
		});
		String[] variables = ModelViewComunicator.getAllVariables();
		combo.add("...");
		for (String string : variables) {
			combo.add(string);
		}
	}
}
